package com.yzf.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzf.common.R;
import com.yzf.dto.SetmealDto;
import com.yzf.entity.Category;
import com.yzf.entity.Dish;
import com.yzf.entity.Setmeal;
import com.yzf.entity.SetmealDish;
import com.yzf.service.CategoryService;
import com.yzf.service.DishService;
import com.yzf.service.SetmealDishService;
import com.yzf.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yzf
 * @description
 * @since 2023/2/16 16:26
 */
@Api(tags = "套餐模块")
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CacheManager cacheManager;

    @ApiOperation(value = "模糊分页查询接口")
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        // 分页构造器
        Page<Setmeal> pg1 = new Page(page, pageSize);
        Page<SetmealDto> pg2 = new Page();
        // 条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        // 添加过滤条件
        queryWrapper.like(name!=null, Setmeal::getName,name);
//        // 添加拍序条件
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealService.page(pg1,queryWrapper);
        BeanUtils.copyProperties(pg1,pg2,"records");
        List<Setmeal> records = pg1.getRecords();
        List<SetmealDto> lists = records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            String name1 = byId.getName();
            setmealDto.setCategoryName(name1);
            return setmealDto;
        }).collect(Collectors.toList());
        pg2.setRecords(lists);
        return R.success(pg2);
    }

    @ApiOperation(value = "新增套餐接口")
    @PostMapping
    @CacheEvict(value = "Setmeal",allEntries = true) //allEntries:删除当前分类下所有数据
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    @ApiOperation(value = "修改套餐接口")
    @PutMapping
    @CacheEvict(value = "Setmeal",key="#setmealDto.categoryId+'_'+#setmealDto.status+''")
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }


    @GetMapping("{id}")
    public R<SetmealDto> GetById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.GetWithId(id);
        return R.success(setmealDto);
    }

    @ApiOperation(value = "删除套餐接口")
    @DeleteMapping
    @CacheEvict(value = "Setmeal",allEntries = true) //allEntries:删除当前分类下所有数据
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteByListId(ids);
        return R.success("删除成功");
    }

    @ApiOperation(value = "套餐条件查询接口")
    @GetMapping("/list")
    @Cacheable(value = "Setmeal",key="#setmeal.categoryId+'_'+#setmeal.status+''")
    public R<List<SetmealDto>> GetList(Setmeal setmeal){
        log.info(setmeal.toString());
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        List<Setmeal> setmeals = setmealService.list(queryWrapper);
        List<SetmealDto> lists=setmeals.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            Long id = item.getId();
            wrapper.eq(SetmealDish::getSetmealId,id);
            List<SetmealDish> list = setmealDishService.list(wrapper);
            setmealDto.setSetmealDishes(list);
            return setmealDto;
        }).collect(Collectors.toList());
        return R.success(lists);
    }

    @GetMapping("/dish/{id}")
    public R<SetmealDto> GetSetalDish(@PathVariable Long id){
        log.info(id.toString());
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        Long categoryId = setmeal.getCategoryId();
        Category byId = categoryService.getById(categoryId);
        String name = byId.getName();
        setmealDto.setCategoryName(name);
        return R.success(setmealDto);
    }

}
